package game;

import java.io.IOException;
import java.net.Socket;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

import graphisme.PaperRockGrapicalInterface;
import player.Player;

public class PaperRock extends SuperGame {

	Player gameWinner = null;
	boolean draw = false;
	PaperRockGrapicalInterface graphicBoard = null;
	int scorePlayer1;
	int scorePlayer2;
	JFrame f = new JFrame();

	public PaperRock(String tmpGame, Player player) {

		// call superclass' constructor
		super(tmpGame, player);

		// set player's number to 2
		setNbJoueur(2);
		setTpt(false);
	}

	public void play() {

		// get last msg from the players
		String currentMsgP1 = playerList.get(0).getCurrentMsg();
		String currentMsgP2 = playerList.get(1).getCurrentMsg();

		compareMove(currentMsgP1, currentMsgP2);

		// if the players make the same move -> no one get a point
		// else the winner gets one point
		if (draw) {
			for (Player p : playerList) {
				sendMsg("draw", p.getBufOut());
			}
		} else if ((gameWinner == playerList.get(0))) {
			scorePlayer1++;
			sendMsg("+1", playerList.get(0).getBufOut());
			sendMsg("+0", playerList.get(1).getBufOut());
		} else if ((gameWinner == playerList.get(1))) {
			scorePlayer2++;
			sendMsg("+1", playerList.get(1).getBufOut());
			sendMsg("+0", playerList.get(0).getBufOut());
		}
		// if one of the players has reached the score of 5, he won
		if (scorePlayer1 == 5) {
			sendMsg("YouLoose", playerList.get(1).getBufOut());
			sendMsg("YouWin", playerList.get(0).getBufOut());
		} else if (scorePlayer2 == 5) {
			sendMsg("YouWin", playerList.get(1).getBufOut());
			sendMsg("YouLoose", playerList.get(0).getBufOut());
		}

		gameWinner = null;
		draw = false;
	}

	// update score board
	public void refreshBoard(String msg) {
		// send score update to both player
		// both player can make a new move
		if (msg.equals("+1")) {
			graphicBoard.setYourTurn(true);
			graphicBoard.setlabelYourScore(1);
		} else if (msg.equals("+0")) {
			graphicBoard.setYourTurn(true);
			graphicBoard.setlabelOpponentScore(+1);
		} else if (msg.equals("draw")) {
			graphicBoard.setYourTurn(true);
		} 
		// message for the winner and the looser
		else if (msg.equals("YouWin")) {
			JOptionPane.showMessageDialog(

			f, "GG my dear", "Information", JOptionPane.PLAIN_MESSAGE);

		} else if (msg.equals("YouLoose")) {
			JOptionPane.showMessageDialog(f, "You loose big noob",
					"Information", JOptionPane.PLAIN_MESSAGE);
		}

	}

	// display score board
	public void displayBoard(Socket socket) throws IOException {
		this.graphicBoard = new PaperRockGrapicalInterface(socket);
		this.graphicBoard.setVisible(true);
	}

	// compare the players' move
	public void compareMove(String move1, String move2) {
		// if the msg are not empty, it means the players have made a move
		if (!move1.equals("") && !move2.equals("")) {

			// if player1 chooses paper
			if (move1.equals("Paper")) {

				// if player2 chooses paper -> draw
				if (move2.equals("Paper")) {
					draw = true;

					// if player2 chooses rock or lizard -> player2 loses
				} else if (move2.equals("Rock") || move2.equals("Spock")) {
					gameWinner = playerList.get(0);

					// if player2 chooses rock or spock -> player2 wins
				} else if (move2.equals("Scissors") || move2.equals("Lizard")) {
					gameWinner = playerList.get(1);
				}

			} else if (move1.equals("Rock")) {
				if (move2.equals("Paper") || move2.equals("Spock")) {
					gameWinner = playerList.get(1);

				} else if (move2.equals("Rock")) {
					draw = true;
				} else if (move2.equals("Scissors") || move2.equals("Lizard")) {
					gameWinner = playerList.get(0);
				}

			} else if (move1.equals("Scissors")) {
				if (move2.equals("Paper") || move2.equals("Lizard")) {
					gameWinner = playerList.get(0);

				} else if (move2.equals("Rock") || move2.equals("Spock")) {
					gameWinner = playerList.get(1);

				} else if (move2.equals("Scissors")) {
					draw = true;
				}
			} else if (move1.equals("Lizard")) {
				if (move2.equals("Paper") || move2.equals("Spock")) {
					gameWinner = playerList.get(0);

				} else if (move2.equals("Rock") || move2.equals("Scissors")) {
					gameWinner = playerList.get(1);

				} else if (move2.equals("Lizard")) {
					draw = true;
				}
			} else if (move1.equals("Spock")) {
				if (move2.equals("Rock") || move2.equals("Scissors")) {
					gameWinner = playerList.get(0);

				} else if (move2.equals("Paper") || move2.equals("Lizard")) {
					gameWinner = playerList.get(1);

				} else if (move2.equals("Spock")) {
					draw = true;
				}
			}

			// reset msg
			playerList.get(0).setCurrentMsg("");
			playerList.get(1).setCurrentMsg("");
		}
	}

	public int getScorePlayer1() {
		return scorePlayer1;
	}

	public void setScorePlayer1(int scorePlayer1) {
		this.scorePlayer1 = scorePlayer1;
	}

	public int getScorePlayer2() {
		return scorePlayer2;
	}

	public void setScorePlayer2(int scorePlayer2) {
		this.scorePlayer2 = scorePlayer2;
	}

}
